package com.cea.papyrus.diagram.sequence.listener;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;

import com.cea.papyrus.core.editpart.GraphEdgeEditPart;
import com.cea.papyrus.core.editpart.GraphNodeEditPart;
import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.sequence.sedi.edit.AbstractEditPart;
import com.cea.papyrus.diagram.sequence.sedi.edit.AbstractLinkEditPart;
import com.cea.papyrus.diagraminterchange2.di2.Property;


/**
 * This class is a listener on GraphElement. It takes in account the type of element and refresh the concerned element.
 */
public class GraphElementListener extends com.cea.papyrus.core.listener.GraphElementListener {

	/**
	 * this class create a listener on the umlElement.
	 * 
	 * @param editPart the editpart of the UMLElement
	 */
	public GraphElementListener(AbstractGraphicalEditPart editPart){
		super(editPart);
	}
	

	@Override
	public void notifyChanged(Notification notification) {
		// EditPart parent =umlEditPart.getParent();
		Debug.debug(this, "Notification GE = "+notification, channel);
		// Refresh the element
		if (notification.getEventType() != Notification.REMOVING_ADAPTER) {
			editPart.refresh();	

			// If this is a label of a link we have to refresh the parent
			if (editPart.getParent() instanceof GraphEdgeEditPart) {
				editPart.getParent().refresh();
			}

			// Default case
			if (notification.getEventType() != Notification.REMOVING_ADAPTER) {
				if (editPart instanceof GraphEdgeEditPart){
					((GraphEdgeEditPart) editPart)
					.getModelManager()
					.modifGraphElement(((GraphEdgeEditPart) editPart).getGraphEdge());
					
				} else if (editPart instanceof GraphNodeEditPart) {
					((GraphNodeEditPart) editPart)
					.getModelManager()
					.modifGraphElement(((GraphNodeEditPart) editPart).getGraphNode());
					
				} else if (editPart instanceof AbstractEditPart<?>) {
					// Temporary solution to handle SeDi EditPart (that don't inherit from GraphNodeEditPart / GraphEdgeEditPart)
					((AbstractEditPart<?>) editPart)
					.getModelManager()
					.modifGraphElement(((AbstractEditPart<?>) editPart).getGraphNode());
					
				} else if (editPart instanceof AbstractLinkEditPart<?>) {
					((AbstractLinkEditPart<?>) editPart)
					.getModelManager()
					.modifGraphElement(((AbstractLinkEditPart<?>) editPart).getGraphEdge());
					
				} else {
					// Not handled EditPart Exception
					Debug.log(new Exception("Unknown EditPart type : "+editPart));
				}
			} 
			
			// Change graph node appearance
			if ( (notification.getEventType() ==  Notification.SET) 
					&& (notification.getNotifier() instanceof Property) ) {
				
				Property aProperty = (Property)notification.getNotifier();
				if (aProperty.getKey().equals(NodeHelper.NODE_FIGURE_APPEARANCE)){
					Debug.debug(this, "CHANGE " + NodeHelper.NODE_FIGURE_APPEARANCE, channel);		
					if( editPart instanceof GraphNodeEditPart){
						((GraphNodeEditPart) editPart).changeEditPartFigure();
					}		
				}		
			}
		}
	}
}